import { Controller, Get, Post, Body, Query, Param, Request } from '@nestjs/common';
import { MenuService } from './menu.service';
import { CreateMenuDto } from './dto/create-menu.dto';
import { CreateActionDto } from './dto/create-action.dto';
import { buildTree } from 'src/utils/fun';
import { TakeToken } from 'src/utils/token';
import { RedisService } from 'src/redis/redis.service';
@Controller('menu')
export class MenuController {
  constructor(
    private readonly menuService: MenuService,
    private RedisService: RedisService
  ) {}

  @Post('save')
  async save(@Body() createMenuDto: CreateMenuDto) {
    const result = await this.menuService.save(createMenuDto);
    return { 
      message: result ? '操作成功': '失败'
    }
  }

  @Get('getMenuList')
  async getMenuList() {
    const result = await this.menuService.findAll();
    if(result){
      return {
        data: buildTree(result)
      }
    }
  }

  @Get('getMenuListByAuth')
  async getMenuListByAuth(@Request() request: any) {
    const info = request.tokenInfo || 0
    if(info){
      const result = await this.menuService.getMenuListByAuth(info.id);
      if(result){
        return {
          data: buildTree(result)
        }
      }
    }
    return []
  }

  @Get('getMenuActionList')
  async getMenuActionList() {
    const result = await this.menuService.getMenuActionList();
    if(result){
      return {
        data: buildTree(result)
      }
    }
  }

  @Post('saveAction')
  async saveAction(@Body() createActionDto: CreateActionDto) {
    const result = await this.menuService.saveAction(createActionDto);
    return { 
      message: result ? '操作成功': '失败'
    }
  }

  @Get('getActionList')
  async getActionList(@Query() query: any) {
    const data = await this.menuService.findActionAll(query);
    if(data){
      return {
        data
      }
    }
  }

  @Get('delete/:id')
  async delete(@Param('id') id: string) {
    const result = await this.menuService.delete(id);
    return { 
      message: result ? '删除成功': '失败'
    }
  }
}
